<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="企业名" prop="name">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="成立日期" prop="establishDate">
        <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="dataForm.establishDate"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
      </el-form-item>
      <el-form-item label="管理员用户名" prop="adminUsername">
        <el-input v-model="dataForm.adminUsername" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="总空间MB" prop="spaceAmount">
        <el-input v-model="dataForm.spaceAmount" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: true,
        roleList: [],
        dataForm: {
          id: 0,
          userName: '',
          password: '',
          comfirmPassword: '',
          salt: '',
          email: '',
          mobile: '',
          roleIdList: [],
          status: 1
        },
        dataRule: {
          name: { required: true, message: '企业名不能为空', trigger: 'blur' },
          establishDate: {
            required: true,
            message: '成立日期不能为空',
            trigger: 'blur'
          },
          adminUsername: {
            required: true,
            message: '管理员账号不能为空',
            trigger: 'blur'
          },
          adminPassword: {
            required: true,
            message: '管理员密码不能为空',
            trigger: 'blur'
          },
          spaceAmount: {
            required: true,
            message: '总空间大小不能为空',
            trigger: 'blur'
          }
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl(`/sys/ent/info/${this.dataForm.id}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.dataForm = data.enterprise
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/ent/update`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
